// 一、配置axios
import router from '../router/index';
// import { useRouter } from 'vue-router';
import axios from 'axios'
import { Notify } from 'vant';
import 'vant/es/notify/style';
const TIME_OUT = 60000;
let baseURl =  process.env.NODE_ENV == 'development'? '/v1' : 'https://api.gaobangbang.com/v1'
const _isMobile = ()=> {
  let flag = navigator.userAgent.match(/(phone|pad|pod|iPhone|iPod|ios|iPad|Android|Mobile|BlackBerry|IEMobile|MQQBrowser|JUC|Fennec|wOSBrowser|BrowserNG|WebOS|Symbian|Windows Phone)/i)
  return flag;
}

const instance = axios.create({
    baseURL: baseURl, 
    timeout: TIME_OUT,
    headers: {
      // "Content-Type": "application/json;charset=UTF-8",
      // "HUOBI-BUSINESS": "PRO",
      // "Huobi-Website": "PRO",
      // "Accept-Language": localStorage.lang ? localStorage.lang : undefined,
    },
  });
instance.interceptors.request.use(
    function (config) {
      if (config.data) {
    //     const o = {};
    //     for (let k in config.data) {
    //       o[snakeCase(k)] = config.data[k];
    //     }
    //     config.data = JSON.stringify(o);
    }
      return config;
    },
    function (error) {
      // Do something with request error
      return Promise.reject(error);
    }
  );
  instance.interceptors.response.use(
    function (response) {
      if (response && response.data) {
        if (Object.keys(response.data).includes("code")) {
          if (response.data.code === 401) {
            // 重新登陆
            if (_isMobile()) {
              Notify({ type: 'warning', message: '用户信息不正确，请重新登录' });
                router.push({ path: '/login'});
              } else {
                Notify({ type: 'warning', message: '用户信息不正确，请重新登录' });
                router.replace('/loginPc');
              }
          }
        }
      }
      return response;
    },
    function (error) {
      return Promise.reject(error);
    }
  );
  
  export default instance;